package type;

import java.util.Hashtable;

import utility.Input;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
//import file.SearchRecord;

public class Report 
{
	DBManager database;
	GetIdName gid;
	String SYSTEMGENERATE="systemgenerate";
	String AUTOUPDATE="autoupdate";
	String SCHEDULE="schedule";
	String Reportid;
	
	public Report(DBManager database,String Reportid)
	{
		this.Reportid = Reportid;
		this.database = database;
		gid = new GetIdName(database);
		SYSTEMGENERATE = gid.getId(SYSTEMGENERATE);
		AUTOUPDATE = gid.getId(AUTOUPDATE);
		SCHEDULE = gid.getId(SCHEDULE);
	}	
	
	public String GettypeofReport()
	{
		//String MASTERPATH = Input.MASTERREPORTSPATH + Reportid + "//" + Reportid + "_mid" + Input.FILRFORMAT;
		//SearchRecord sr = new SearchRecord(MASTERPATH);
		Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		table.put("mid", Reportid);
		table.put("pid",SCHEDULE);
		table.put("td","null");
		/*sr.fileOpen();
		String line[] = sr.getRecordSet(table);
		sr.fileClose();*/
		
		RecordSearch ps = new RecordSearch(database);
		ps.setConditionMap(table);
		String line[] = ps.getArrayFromResultSet();
		if(line.length>0)  
			return "schedule";
		table.put("pid",AUTOUPDATE);
		
		//line = sr.getRecordSet(table);
		ps.setConditionMap(table);
		line = ps.getArrayFromResultSet();
		if(line.length>0) 
			return "autoupdate";
		table.put("pid",SYSTEMGENERATE);
		//line = sr.getRecordSet(table);
		ps.setConditionMap(table);
		line = ps.getArrayFromResultSet();
		
		if(line.length>0) 
			return "systemgenerate";
		return "null";
	}
	
	public Hashtable<Object,Object> getReportMap()
	{
	//String MASTERPATH = Input.MASTERREPORTSPATH +  Reportid + "//" + Reportid + "_mid" + Input.FILRFORMAT;
		//SearchRecord sr = new SearchRecord(MASTERPATH);
		Hashtable<Object, Object> table = new Hashtable<Object, Object>();
		table.put("mid",Reportid);
		table.put("td","null");
		/*sr.fileOpen();
		String line[] = sr.getRecordSet(table);
		sr.fileClose();*/
		
		RecordSearch ps = new RecordSearch(database);
		ps.setConditionMap(table);
		String line[] = ps.getArrayFromResultSet();
		table.clear();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value =  splitline[Column.pv_index-1];
			if(value.equals("0"))
				value =  splitline[Column.vt_index-1];
			else
				value = gid.getItem(value);
			table.put(gid.getItem(splitline[Column.pid_index-1]),value);
		}
		return table;
	}
}
